package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.AppLL;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.CityMode;
import com.sdcqjy.property.mode.ImageMode;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.contract.AboutContract;
import com.sdcqjy.property.presenter.contract.a;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 磁磁帅 on 2018/4/25.
 * 功能：
 */

public class AboutPresenter extends BaseContract.Presenter<AboutContract.View> {
    public AboutPresenter(AboutContract.View baseView) {
        super(baseView);
    }

    public void getImageList() {
        Map<String, Object> map = new HashMap<>();
        map.put("imageType", "" + 2);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetImageList, new BaseRequestBack<BaseRet<List<ImageMode>>>() {
                    public BaseRet<List<ImageMode>> jsonToMode(String jsonStr) {
                        BaseRet<List<ImageMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<ImageMode>>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<List<ImageMode>> ret) {
                        if (baseView != null) {
                            //baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getImageListRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            //baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        //if (baseView != null) baseView.openLoadDialog();
    }

    public void getDetailsMode(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetDetails, new BaseRequestBack<BaseRet<ListMsgMode>>() {
                    public BaseRet<ListMsgMode> jsonToMode(String jsonStr) {
                        BaseRet<List<ListMsgMode>> retList = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<ListMsgMode>>>() {
                        }.getType());
                        BaseRet<ListMsgMode> ret = new BaseRet<>();
                        ret.success = retList.success;
                        ret.code = retList.code;
                        ret.msg = retList.msg;
                        if (ret.success) {
                            List<ListMsgMode> data = retList.data;
                            if (data != null && data.size() > 0) ret.data = data.get(0);
                        }
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<ListMsgMode> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getDetailsModeRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void getHtml() {
        Map<String, Object> map = new HashMap<>();
        map.put("categoryID", "" + 7);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetDetails, new BaseRequestBack<BaseRet<ListMsgMode>>() {
                    public BaseRet<ListMsgMode> jsonToMode(String jsonStr) {
                        BaseRet<List<ListMsgMode>> retList = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<ListMsgMode>>>() {
                        }.getType());
                        BaseRet<ListMsgMode> ret = new BaseRet<>();
                        ret.success = retList.success;
                        ret.code = retList.code;
                        ret.msg = retList.msg;
                        if (ret.success) {
                            List<ListMsgMode> data = retList.data;
                            if (data != null && data.size() > 0) ret.data = data.get(0);
                        }
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<ListMsgMode> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.getHtmlRet(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void openDetailsOfListOne(int type) {
        CityMode mode = CityMode.getMode(AppLL.getAppLL());
        Map<String, Object> map = new HashMap<>();
        map.put("categoryID", "" + type);
        map.put("projectID", mode.id);
        new HttpClient().setHttpType(HttpClient.HttpType.JSON).setParam(map)
                .httpRequest(a.GetWordList, new BaseRequestBack<BaseRet<List<ListMsgMode>>>() {
                    public BaseRet<List<ListMsgMode>> jsonToMode(String jsonStr) {
                        BaseRet<List<ListMsgMode>> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<List<ListMsgMode>>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<List<ListMsgMode>> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                List<ListMsgMode> data = ret.data;
                                if (data == null) data = new ArrayList<>();
                                if (data.size() == 0) {
                                    baseView.applyError("暂无数据");
                                } else {
                                    ListMsgMode mode1 = data.get(0);
                                    getDetailsMode(mode1.id);
                                }
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }
}
